{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ***源代码***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "累加求和 45\n"
     ]
    }
   ],
   "source": [
    "R = eval(input(\"请输入正整数:\"))\n",
    "i, S = 0, 0\n",
    "while (i<=R):\n",
    "    S = S + i\n",
    "    i = i + 1\n",
    "print(\"累加求和\",S)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ***Graphviz的实现***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 12.2.0 (20241103.1931)\n",
       " -->\n",
       "<!-- Title: 循环结构 Pages: 1 -->\n",
       "<svg width=\"257pt\" height=\"404pt\"\n",
       " viewBox=\"0.00 0.00 257.19 404.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 400)\">\n",
       "<title>循环结构</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-400 253.19,-400 253.19,4 -4,4\"/>\n",
       "<!-- 1 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>1</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"138.31,-396 84.31,-396 84.31,-360 138.31,-360 138.31,-396\"/>\n",
       "<text text-anchor=\"middle\" x=\"111.31\" y=\"-371.82\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">开始</text>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>2</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"149.31,-324 73.31,-324 73.31,-288 149.31,-288 149.31,-324\"/>\n",
       "<text text-anchor=\"middle\" x=\"111.31\" y=\"-300.95\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">i = 0,S = 0</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;2 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>1&#45;&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M111.31,-359.7C111.31,-352.41 111.31,-343.73 111.31,-335.54\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"114.81,-335.62 111.31,-325.62 107.81,-335.62 114.81,-335.62\"/>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>3</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"222.62,-252 45.55,-252 0,-216 177.07,-216 222.62,-252\"/>\n",
       "<text text-anchor=\"middle\" x=\"111.31\" y=\"-228.95\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">输入R的数值</text>\n",
       "</g>\n",
       "<!-- 2&#45;&gt;3 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>2&#45;&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M111.31,-287.7C111.31,-280.41 111.31,-271.73 111.31,-263.54\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"114.81,-263.62 111.31,-253.62 107.81,-263.62 114.81,-263.62\"/>\n",
       "</g>\n",
       "<!-- 4 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>4</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"111.31,-180 60.4,-162 111.31,-144 162.22,-162 111.31,-180\"/>\n",
       "<text text-anchor=\"middle\" x=\"111.31\" y=\"-156.95\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">i &lt;= R</text>\n",
       "</g>\n",
       "<!-- 3&#45;&gt;4 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>3&#45;&gt;4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M111.31,-215.7C111.31,-208.41 111.31,-199.73 111.31,-191.54\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"114.81,-191.62 111.31,-181.62 107.81,-191.62 114.81,-191.62\"/>\n",
       "</g>\n",
       "<!-- 5 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>5</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"83.44,-108 17.19,-108 17.19,-72 83.44,-72 83.44,-108\"/>\n",
       "<text text-anchor=\"middle\" x=\"50.31\" y=\"-84.95\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">S = S + i</text>\n",
       "</g>\n",
       "<!-- 4&#45;&gt;5 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>4&#45;&gt;5</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M99.84,-147.83C92.23,-139.11 82,-127.37 72.81,-116.82\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"75.63,-114.72 66.42,-109.48 70.35,-119.32 75.63,-114.72\"/>\n",
       "</g>\n",
       "<!-- 7 -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>7</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"249.19,-108 139.44,-108 139.44,-72 249.19,-72 249.19,-108\"/>\n",
       "<text text-anchor=\"middle\" x=\"194.31\" y=\"-84.95\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">输出S的数值</text>\n",
       "</g>\n",
       "<!-- 4&#45;&gt;7 -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>4&#45;&gt;7</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M125.77,-148.81C136.66,-139.62 151.97,-126.71 165.35,-115.43\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"167.26,-118.4 172.64,-109.27 162.74,-113.05 167.26,-118.4\"/>\n",
       "</g>\n",
       "<!-- 6 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>6</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"112.19,-36 50.44,-36 50.44,0 112.19,0 112.19,-36\"/>\n",
       "<text text-anchor=\"middle\" x=\"81.31\" y=\"-12.95\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">i = i + 1</text>\n",
       "</g>\n",
       "<!-- 5&#45;&gt;6 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>5&#45;&gt;6</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M57.97,-71.7C61.35,-64.07 65.4,-54.92 69.18,-46.4\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"72.27,-48.05 73.12,-37.49 65.87,-45.21 72.27,-48.05\"/>\n",
       "</g>\n",
       "<!-- 6&#45;&gt;4 -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>6&#45;&gt;4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M84.99,-36.43C90.11,-60.65 99.43,-104.75 105.49,-133.45\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"102.06,-134.17 107.56,-143.23 108.91,-132.72 102.06,-134.17\"/>\n",
       "</g>\n",
       "<!-- 8 -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>8</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"221.31,-36 167.31,-36 167.31,0 221.31,0 221.31,-36\"/>\n",
       "<text text-anchor=\"middle\" x=\"194.31\" y=\"-11.82\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">结束</text>\n",
       "</g>\n",
       "<!-- 7&#45;&gt;8 -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>7&#45;&gt;8</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M194.31,-71.7C194.31,-64.41 194.31,-55.73 194.31,-47.54\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"197.81,-47.62 194.31,-37.62 190.81,-47.62 197.81,-47.62\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x1e26c9ae990>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import graphviz\n",
    "dot=graphviz.Digraph(comment='the round table',name=\"循环结构\",node_attr={'shape': 'box'})\n",
    "dot.node('1','开始')\n",
    "dot.node('2','i = 0,S = 0')\n",
    "dot.node('3','输入R的数值',shape='parallelogram')\n",
    "dot.node('4','i <= R',shape='diamond')\n",
    "dot.node('5','S = S + i')\n",
    "dot.node('6','i = i + 1')\n",
    "dot.node('7','输出S的数值')\n",
    "dot.node('8','结束')\n",
    "dot.edges(['12','23','34','45','56','64','47','78'])\n",
    "dot"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
